package com.jerkin.bean;

import com.jerkin.model.User;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

import java.net.MalformedURLException;

/**
 * @version V1.0
 * @ClassName: LoadBean
 * @Description:
 * @author: huhongzhi
 * @date: 2020/10/18 22:48
 */
public class LoadBeanDemo {

    public static void main(String[] args) throws MalformedURLException {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:spring-bean.xml");
//        User user1 = applicationContext.getBean(User.class);
//        System.out.println(user1);
//        ClassPathResource classPathResource =
//                new ClassPathResource("spring-bean.xml",LoadBeanDemo.class.getClassLoader());
        UrlResource resource = new UrlResource("file:/F:/hhz/mayun/jerkin/jerkin-spring/target/classes/spring-bean.xml");
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        reader.loadBeanDefinitions(resource);
        User user = beanFactory.getBean(User.class);
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition("user");
        System.out.println(beanDefinition);
        System.out.println(user.toString());
    }
}
